package cn.npe.di.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * @ClassName InvokeAssignHandlerImpl
 * @Author zhjx
 * @Description  掉用指定的实现类
 * @Date 2024/5/17 17:42
 **/
public class InvokeAssignHandlerImpl implements InvokeHandler,ApplicationContextAware{
    private ApplicationContext applicationContext;
    @Override
    public boolean support(String type) {
        return "assign".equalsIgnoreCase(type);
    }

    @Override
    public Object invoke(Object bean, Method method, Object[] args, Class<?> type, String[] serviceIds) {
        //是指定某个实现类 直接走spring容器根据那么获取后调用
        for (String serviceId:serviceIds){
            try {
                method.invoke(applicationContext.getBean(serviceId),args);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
